<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/8
 * Time: 13:35
 */

namespace app\admin\model;

use think\Model;
use think\Db;
class CashModel extends Model{
    // 确定链接表名
    protected $table = 'snake_user_cash';
    function getAll($map){
        return $this->where($map)->count();
    }

    function getOrder($map,$offset,$limit){
        return $this->where($map)->limit("$offset , $limit")->order('addtime DESC,cash_id DESC')->select();
    }

    function pass($id){
        try{
            $userCash=$this->where(array('cash_id'=>$id))->find();
            if($userCash['status']==2){
                return msg(-1, '', '此订单已提现,不可重复提现');
            }
            $result=true;
            $this->save(array('status'=>2,'check_time'=>time()),['cash_id'=>$id]);
            /*
            $user=Db::name('users')->field('vip,vip_time,pay_deposit,deposit')->where(array('user_id'=>$userCash['user_id']))->find();
            $deposit=$user['deposit']-$userCash['money'];
            if($deposit==0){
                $map=array('deposit'=>0,'pay_deposit'=>0);
            }else{
                $map=array('deposit'=>$deposit);
            }
            $result=Db::name('users')->where(array('user_id'=>$userCash['user_id']))->update($map);
            */
            if(false === $result){
                // 验证失败 输出错误信息
                return msg(-1, '', $this->getError());
            }else{

                return msg(1, url('cash/index'), '审核成功');
            }
        }catch(\Exception $e){
            return msg(-2, '', $e->getMessage());
        }
    }

    function reject($id){
        try{
            $userCash=$this->where(array('cash_id'=>$id))->find();
            if($userCash['status']==2){
                return msg(-1, '', '此订单已提现,不可驳回');
            }
            $result=true;
            if($userCash['status']==1) {
                /*
                $user=Db::name('users')->field('vip,vip_time,pay_deposit,deposit')->where(array('user_id'=>$userCash['user_id']))->find();
                $deposit=$user['deposit']+$userCash['money'];
                if($deposit==0){
                    $map=array('deposit'=>0,'pay_deposit'=>0);
                }else{
                    $map=array('deposit'=>$deposit,'pay_deposit'=>1);
                }
                $result=Db::name('users')->where(array('user_id'=>$userCash['user_id']))->update($map);
                */
            }
            if(false === $result){
                // 验证失败 输出错误信息
                return msg(-1, '', $this->getError());
            }else{

                return msg(1, url('cash/index'), '已驳回申请');
            }
        }catch(\Exception $e){
            return msg(-2, '', $e->getMessage());
        }
    }
}